<?php
namespace app\index\controller;

use aliyun\Sms;
use app\index\model\User;
use think\Controller;
use think\Loader;
use think\captcha\Captcha;

class Reg extends Common
{
    public function index()
    {
    	$this->assign('title',"注册");
		return $this->fetch();
    }

    public function code()
    {
    	$config =    [
		    // 验证码字体大小
		    'fontSize'    =>    25,    
		    // 验证码位数
		    'length'      =>    3,   
		    // 关闭验证码杂点
		    'useNoise'    =>    false, 
		    'imageH'      =>  50,
		    'useCurve'    =>  false
		];
		$captcha = new Captcha($config);
		return $captcha->entry();
    }

    public function sendmsg()
    {
    	$data = input('post.');

    	if(session('msginfo')){
    		if(time()-session('msginfo')['create_time']<10)
    			$this->error('一分钟之内不能重复发');
    	}

    	$validate = Loader::validate('User');

		if(!$validate->scene('send')->check($data)){

			$this->error($validate->getError());
		}

		$code = mt_rand(1000,9999);

		// Sms::send($data['phone'],$code);

		$info = [
			'phone'=>$data['phone'],
			'code'=>$code,
			'create_time'=>time()
		];

		// session('send_num',session('send_num')+1);
		session('msginfo',$info);

    	$this->success("发送成功");
    }

    public function test()
    {
    	var_dump(session('msginfo'));
    }

    public function checkreg()
    {
    	$data = input('post.');

    	$validate = Loader::validate('User');

		if(!$validate->check($data)){

			$this->error($validate->getError());
		}

		$data['password'] = md5($data['password']);

		$data['face'] = "static/index/images/face.jpg";

		$res = User::create($data);

		session('msginfo',null);

		if($res){
			$this->success("注册成功");
		}else{
			$this->error('注册失败，请稍后再试');
		}

    }
}
